package com.woniu.adopt.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.adopt.mapper.ManagerMapper;
import com.woniu.adopt.entity.Manager;
import com.woniu.adopt.mapper.TeamMapper;
import com.woniu.adopt.vo.ManagerVo;
import com.woniu.adopt.vo.TeamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ManagerService {

    @Autowired
    ManagerMapper managerMapper;
    @Autowired
    TeamMapper teamMapper;

    public Manager login(String name) {
        return managerMapper.selectManageByName(name);
    }

    public PageInfo<ManagerVo> queryByPage(Integer now, Integer size, String name) {
        PageHelper.startPage(now, size);
        List<ManagerVo> list = managerMapper.selectManagerByName(name);
        return new PageInfo<>(list);
    }

    @Transactional
    public void add(ManagerVo managerVo) {
        //1. 添加管理员
        managerMapper.insertManager(managerVo); //插入之后 返回当前管理员的 ID
        //2. 授予角色
        managerMapper.insertRoleToManager(managerVo.getId(), managerVo.getRoleIds());
    }

    @Transactional
    public void modify(ManagerVo managerVo) {
        //0.先删除当前管理员拥有的所有角色
        managerMapper.removeRoleByMid(managerVo.getId());
        //1. 添加管理员的角色
        managerMapper.insertRoleToManager(managerVo.getId(), managerVo.getRoleIds());
        //2. 修改管理员
        managerMapper.updateByPrimaryKey(managerVo);
    }

    @Transactional
    public void removeByKey(int mid) {
        //0.先删除当前管理员拥有的所有角色
        managerMapper.removeRoleByMid(mid);
        //1. 删除管理员,执行逻辑删除
        ManagerVo manager = new ManagerVo();
        manager.setId(mid);
        manager.setDel(1);
        manager.setUpdateTime(new Date());
        managerMapper.updateByPrimaryKey(manager);
    }

    public List<String> getRoleNamesByMid(int mid) {
        return managerMapper.selectRoleNamesByMid(mid);
    }

    public int changeLocked(int id) {
        //切换locked的值
        Manager manager = managerMapper.selectByPrimaryKey(id);
        Integer locked = manager.getLocked();
        ManagerVo managerVo = new ManagerVo();
        managerVo.setId(id);
        managerVo.setLocked(locked == 0 ? 1 : 0);
        managerVo.setUpdateTime(new Date());
        return managerMapper.updateByPrimaryKey(managerVo);
    }

    public PageInfo<TeamVo> queryTeamByPage(Integer now, Integer size, String name) {

        List<TeamVo> teamVos = teamMapper.selectTeamByName(name);
        List<TeamVo> temp = new ArrayList<>();
        teamVos.forEach(teamVo -> {
            List<Integer> list = new ArrayList();
            teamVo.getRoleList().forEach(r -> {
                        if ("超级管理员".equals(r.getName())) {
                            list.add(0);
                        } else if ("普通管理员".equals(r.getName())) {
                            list.add(1);
                        } else if ("救助站工作人员".equals(r.getName())) {
                            list.add(2);
                        } else if ("志愿者".equals(r.getName())) {
                            list.add(3);
                        } else if ("特邀作家".equals(r.getName())) {
                            list.add(4);
                        }
                    }
            );
            //list为空，说明非团队成员从团队中排除掉,在迭代器直接移除会报出来了一个异常：ConcurrentModificationException，原因是使用迭代器迭代过程中改变了迭代集合中的元素
            //解决办法：待移除数据先暂存到temp，然后在最后移除
            if (list.size()==0) {
                temp.add(teamVo);
            } else {
                int min = Collections.min(list);
                switch (min) {
                    case 0: {
                        teamVo.setTypeName("队长");
                        teamVo.setType(0);
                        break;
                    }
                    case 1: {
                        teamVo.setTypeName("组员");
                        teamVo.setType(1);
                        break;
                    }
                    case 2: {
                        teamVo.setTypeName("救护站工作人员");
                        teamVo.setType(2);
                        break;
                    }
                    case 3: {
                        teamVo.setTypeName("志愿者");
                        teamVo.setType(3);
                        break;
                    }
                    case 4: {
                        teamVo.setTypeName("特邀作家");
                        teamVo.setType(4);
                        break;
                    }
                    default: {
                        break;
                    }
                }

            }
        });
        /*这个写法[idea提示]同
        temp.forEach(t->{
         teamVos.remove(t);})
         */
        temp.forEach(teamVos::remove);
        System.out.println(teamVos);
        //PageHelper需要挪到这里！不然憨批乱分页！！！！
        PageHelper.startPage(now, size);
        return new PageInfo<>(teamVos);
    }

    public int modifyTeam(TeamVo teamVo) {
        return teamMapper.updateByPrimaryKey(teamVo);
    }

    public void addTeam(TeamVo teamVo) {
        teamMapper.insert(teamVo);
    }

    public int removeTeamByKey(int id) {
        TeamVo teamVo = new TeamVo();
        teamVo.setId(id);
        teamVo.setDel(1);
        return teamMapper.updateByPrimaryKey(teamVo);
    }

    public PageInfo<TeamVo> queryTeamByPageOld(Integer now, Integer size, String name) {
        PageHelper.startPage(now, size);
        List<TeamVo> list = teamMapper.selectTeamOldByName(name);
        return new PageInfo<>(list);
    }

    public int recycleTeamByKey(int id) {
        TeamVo teamVo = new TeamVo();
        teamVo.setId(id);
        teamVo.setDel(0);
        return teamMapper.updateByPrimaryKey(teamVo);
    }

    public int judgedSameName(String userName) {
        return managerMapper.judgedSameName(userName);
    }
}
